package nautilus.placetaker.dao;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import nautilus.placetaker.dto.Place;

public class DataFileUtils {
	
	public static boolean exportPlacesToCSV(ArrayList<Place> trans, 
			String filePath) throws IOException{
		FileWriter fw = null;
		String dataLine = "";
		Place tr;
		try{
			fw = new FileWriter(filePath);
			BufferedWriter bw = new BufferedWriter(fw);
			dataLine = "Id,Name,Address,Latitude,Longitude,Image1,Image2,Image3,Image4,CategoryId,CategoryName,LastUpdate,Audio,Description,isCheck\n";
			bw.write(dataLine);
			for(int i=0; i<trans.size(); i++){
				tr = trans.get(i);
				dataLine = tr.getId() + ",\"" + tr.getName() + "\",\"" + tr.getAddress() + 
						"\"," + tr.getLatitude() + "," + tr.getLongitude() +
						",\"" + tr.getImage(0) + "\",\"" + tr.getImage(0) +"\",\"" + tr.getImage(0) + "\",\"" + tr.getImage(0) + 
						"\"," + tr.getCategoryId() + ",\"" + tr.getCategoryName() + 
						"\",\"" + tr.getLastUpdate() + 
						"\",\"" + tr.getAudioFile() +
						"\",\"" + tr.getDescription() + 
						"\"," + tr.isCheck();
				bw.write(dataLine + "\n");
			}
			bw.flush();
			return true;
		}catch(IOException ex){
			return false;
		}
		finally{
			if(fw != null)
				fw.close();
		}
	}
}
